package com.polaris.live.ui.message.widget.single_message

import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.viewbinding.ViewBinding
import com.bumptech.glide.Glide
import com.lxj.xpopup.XPopup
import com.polaris.live.R
import com.polaris.live.adapter.MessageAdapter
import com.polaris.live.common.cacha.CacheUtil
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.im.db.entity.MessageEntity
import com.polaris.live.im.sfs.MESSAGE_STATUS_ERROR
import com.polaris.live.im.sfs.MESSAGE_STATUS_SENDING
import com.polaris.live.im.sfs.MESSAGE_STATUS_SUCCESS
import com.polaris.live.im.sfs.MESSAGE_STATUS_TIMEOUT
import com.polaris.live.im.sfs.SYSTEM_USER_ID
import com.polaris.live.ui.message.utils.MessageAdapterUtils
import com.polaris.live.utils.DecorationUtils
import com.polaris.live.widget.avatar.AvatarFrameView
import com.polaris.live.widget.avatar.AvatarView
import com.polaris.live.widget.avatar.FrameImageUtils
import com.polaris.live.widget.bubble.BubbleView
import com.polaris.live.widget.refresh.BaseBindingViewHolder

/**
 * AbstractMessageItemHolder
 *
 * @author Created by lucas on 2023/10/22 15:43
 */
abstract class AbstractMessageItemHolder<VB : ViewBinding>(
    val mAdapter: MessageAdapter,
    mBinding: VB,
) : BaseBindingViewHolder<VB, MessageEntity>(mBinding) {

    val builder: XPopup.Builder = XPopup.Builder(mBinding.root.context)
        .hasShadowBg(false)
        .isViewMode(true)
        .watchView(itemView)

    //绑定通用数据
    fun bindCommon(
        item: MessageEntity,
        timeView: TextView,
        leftLayout: ViewGroup,
        rightLayout: ViewGroup,
        leftAvatar: AvatarView,
        rightAvatar: AvatarView,
        leftFrame: AvatarFrameView,
        rightFrame: AvatarFrameView,
        leftBubble: BubbleView?,
        rightBubble: BubbleView?,
        stateView: ImageView? = null,
        errorView: TextView? = null,
    ) {
        MessageAdapterUtils.fillTimeStamp(
            mAdapter.data,
            timeView,
            layoutPosition,
            item.timestamp
        )

        if (item.sender != UserManager.getUser().userId) {
            leftLayout.visibility = View.VISIBLE
            rightLayout.visibility = View.GONE

            if (item.sender == SYSTEM_USER_ID) {
                leftAvatar.setImageResource(R.drawable.ic_round_launcher)
            } else {
                leftAvatar.setImageUrl(mAdapter.getAvatar())
                leftFrame.setFrameImage(FrameImageUtils.urlToFrameImageBean(mAdapter.getAvatarFrame()))
                leftBubble?.setBubble(DecorationUtils.getDecorationChatBubble(mAdapter.getChatBubble()))
            }

        } else {
            leftLayout.visibility = View.GONE
            rightLayout.visibility = View.VISIBLE

            rightAvatar.setImageUrl(UserManager.getUser().avatar, UserManager.getUserId())
            rightFrame.setFrameImage(FrameImageUtils.urlToFrameImageBean(UserManager.getUser().decoration?.avatarFrame))
            rightBubble?.setBubble(DecorationUtils.getDecorationChatBubble(UserManager.getUser().decoration?.chatBubble))
        }

        if (stateView != null && errorView != null) {
            bindState(item, stateView, errorView)
        }
    }

    private fun bindState(
        item: MessageEntity,
        stateView: ImageView,
        errorView: TextView,
    ) {
        stateView.isSelected = false
        when (item.state) {
            MESSAGE_STATUS_SENDING -> {
                stateView.visibility = View.VISIBLE
                errorView.visibility = View.GONE

                Glide.with(mBinding.root.context)
                    .asGif()
                    .load(R.drawable.message_item_state_loading)
                    .into(stateView)
            }

            MESSAGE_STATUS_SUCCESS -> {
                stateView.visibility = View.INVISIBLE
                val remindMessage = item.parameterBean?.remindMessage
                if (remindMessage?.isNotEmpty() == true) {
                    errorView.visibility = View.VISIBLE
                    errorView.text = remindMessage.joinToString(separator = "\n\n") { it.content ?: "" }
                } else {
                    errorView.visibility = View.GONE
                }
            }

            MESSAGE_STATUS_ERROR -> {
                stateView.visibility = View.VISIBLE
                if (item.respErrorMessage != null) {
                    //如果大于则还可以重试
                    if (item.timestamp + (item.retryExpiredTime
                            ?: 0) >= CacheUtil.getServerTime()
                    ) {
                        errorView.visibility = View.GONE
                        stateView.setImageResource(R.drawable.message_item_state_retry)
                        stateView.isSelected = true
                    } else {
                        errorView.visibility = View.VISIBLE
                        stateView.setImageResource(R.drawable.message_item_state_error)
                        errorView.text = item.respErrorMessage
                    }
                } else {
                    errorView.visibility = View.VISIBLE
                    stateView.setImageResource(R.drawable.message_item_state_error)
                    errorView.text = item.errorMessage
                }
            }

            MESSAGE_STATUS_TIMEOUT -> {
                stateView.visibility = View.VISIBLE
                errorView.visibility = View.GONE
                stateView.setImageResource(R.drawable.message_item_state_retry)
                stateView.isSelected = true
            }
        }
    }
}